<template>
    <div>
        <el-form :model="oneBuy" label-width="100px" style="text-align: left;">
            <el-row>
                <el-col :span="6">
                    <el-form-item label="设备">
                        <el-select v-model="oneBuy.stockid" placeholder="请选择设备" style="width: 100%;">
                            <el-option v-for="base in basestocks" :label="base.name" :value="base.id" :key="base.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="6">
                    <el-form-item label="使用日期">
                        <el-date-picker type="date" v-model="oneBuy.usedate" style="width: 100%" placeholder="请选择日期">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="购买日期">
                        <el-date-picker type="date" v-model="oneBuy.buydate" style="width: 100%" placeholder="请选择日期">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="6">
                    <el-form-item label="购买数量(件)" prop="buysum">
                        <el-input v-model="oneBuy.buysum" @change="money" type="number" placeholder="请输入购买数量"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="商品单价(元)" prop="price">
                        <el-input v-model="oneBuy.price" @change="money" type="number" placeholder="请输入商品单价"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="6">
                    <el-form-item label="金额(元)">
                        <el-input v-model="oneBuy.money" :disabled="true"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="购买人">
                        <el-input v-model="oneBuy.buyman" placeholder="请填写您的姓名"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="6">
                    <el-form-item label="描述">
                        <el-input type="textarea" v-model="oneBuy.description" :rows="5" placeholder="请填写采购细节"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item>
                <el-button type="primary" @click="dialogVisible = true">添 加</el-button>
                <el-button @click="reset()">重 置</el-button>
            </el-form-item>
        </el-form>

        <el-dialog :visible.sync="dialogVisible" width="15%">
            <span>
                <font style="font-size: 16px;">确定提交？</font>
            </span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addBuy()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            oneBuy: {
                buyman: "",
                stockid: "",
                usedate: "",
                buysum: "",
                buydate: "",
                description: "",
                money: "",
                price: "",
            },
            dialogVisible: false,
            basestocks: [],
        };
    },
    methods: {
        addBuy() {
            this.oneBuy.buydate = '2025-01-17';
            this.oneBuy.usedate = '2025-01-17';
            this.$axios.get("/buycheck/addCheck?buyman=" + this.oneBuy.buyman + "&stockid=" + this.oneBuy.stockid + "&usedate=" + this.oneBuy.usedate + "&buysum=" + this.oneBuy.buysum + "&description=" + this.oneBuy.description + "&money=" + this.oneBuy.money + "&price=" + this.oneBuy.price)
                .then((result) => {
                    if (result.data.code == 200) {
                        this.$message({
                            message: result.data.message,
                            type: 'success'
                        });
                        this.dialogVisible = false;
                        this.showList();
                    } else {
                        this.$message.error(result.data.message);
                    }
                }).catch((err) => {
                    console.log(err);
                });
        },
        money() {
            this.oneBuy.money = this.oneBuy.buysum * this.oneBuy.price;
        },
        reset() {
            this.oneBuy = {
                buyman: "",
                stockid: "",
                usedate: "",
                buysum: "",
                buydate: "",
                description: "",
                money: "",
                price: "",
            };
        },
        showList() {
            this.$axios.get("/base/selectBaseStockAll?pageNum=1&pageSize=100")
                .then((res) => {
                    this.basestocks = res.data.data.list;
                    for (let i = 0; i < this.basestocks.length; i++) {
                        this.basestocks[i].usedate = this.basestocks[i].usedate.substring(0, 10);
                        this.basestocks[i].buydate = this.basestocks[i].buydate.substring(0, 10);
                    }
                })
                .catch((error) => {
                    console.log(error);
                });
        },
    },
    created() {
        this.showList();
    },
};
</script>

<style></style>